package com.wc.AlgoOJ.CT0212;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/1/24 20:30
 * @description blg玩贪吃蛇Ⅱ
 * http://43.138.190.70:8888/p/CT0212
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 2010, M = 200010;
    // 存的身子
    static boolean[][] grip = new boolean[N][N];
    // 存食物
    static boolean[][] food = new boolean[N][N];
    static int[][] foodTmp = new int[M][3];
    static int n, x, y, t;
    static String s;
    static int[] op = new int[128];
    static int[] posX = new int[]{1, -1, 0, 0};
    static int[] posY = new int[]{0, 0, -1, 1};

    public static void main(String[] args) {
        s = sc.next();
        n = sc.nextInt();
        op['w'] = 0;
        op['s'] = 1;
        op['a'] = 2;
        op['d'] = 3;
        for (int i = 1; i <= n; i++) {
            x = sc.nextInt() + 1000;
            y = sc.nextInt() + 1000;
            t = sc.nextInt();
            foodTmp[i][0] = x;
            foodTmp[i][1] = y;
            foodTmp[i][2] = t;
        }

        Arrays.sort(foodTmp, 1, n + 1, Comparator.comparingInt(o -> o[2]));
        // 当前豆子出现的位置
        int id = 1;
        int headX = 1000;
        int headY = 1000;
        // 身体
        LinkedList<Integer> body = new LinkedList<>();
        body.add(headX);
        body.add(headY);
        grip[headX][headY] = true;
        char[] chs = s.toCharArray();
        int time = 1;
        // 移动
        for (char ch : chs) {
            // 先放豆子
            while (id <= n && foodTmp[id][2] == time) {
                x = foodTmp[id][1];
                y = foodTmp[id][0];
                food[x][y] = true;
                id++;
            }
            int preHeadX = body.get(0);
            int preHeadY = body.get(1);
            int lastY = 0, lastX = 0;
            // 接下来走的路
            headX = preHeadX + posX[op[ch]];
            headY = preHeadY + posY[op[ch]];
            // 是否是吃豆子才碰到的身体
            boolean flg = false;
            // 判断墙壁
            if (isDied(headX, headY)) break;
            // 判断当前是否为食物
            if (food[headX][headY]) {
                // 吃掉了
                food[headX][headY] = false;
            } else {
                flg = true;
                // 移动尾部先
                lastY = body.removeLast();
                lastX = body.removeLast();
                grip[lastX][lastY] = false;
            }
            // 碰到身子了
            if (grip[headX][headY]) {
                // 如果是移除尾巴然后碰到身体，要把尾巴补回来
                if (flg) {
                    body.add(lastX);
                    body.add(lastY);
                }
                break;
            }
            // 就把头加进去
            body.addFirst(headY);
            body.addFirst(headX);
            // 身子++
            grip[headX][headY] = true;
            // 时间
            time++;
        }

        out.println(body.size() / 2);
        out.flush();
    }

    static boolean isDied(int x, int y) {
        return x < 0 || x > 2000 || y < 0 || y > 2000;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
